package com.neuedu.his.service;

import com.neuedu.his.dao.MenuDao;
import com.neuedu.his.dao.RoleDao;
import com.neuedu.his.entity.Menu;
import com.neuedu.his.entity.Role;
import com.neuedu.his.entity.User;
import com.neuedu.his.framework.BaseService;
import com.neuedu.his.framework.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       RoleService
 * 创建时间：  2020/12/23  8:52
 * 描述 :     用户测试类
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class MenuService implements BaseService<Menu> {
    private MenuDao menuDao = new MenuDao();


    /**
     * 查询所有菜单  以父子关系 返回
     * @param request
     * @return
     */
    @Override
    public List<Menu> queryList(HttpServletRequest request) {
        //只查询 一级菜单
        String sql = " select * from menu  where parent_id is null and  active = 1   ";
        List<Menu> list =  menuDao.selectList(sql);

        queryChild(list);

        return list;
    }


    /**
     * 分页查询
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public PageInfo queryListByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        PageInfo pageInfo = getPageInfo(request);

        String menuName = request.getParameter("menuName");

        if(menuName == null ){
            menuName = "";
        }

        menuName = "%"+menuName.trim()+"%";



        //只查询 一级菜单
        String sql = " select * from menu  where parent_id is null and  active = 1 and menu_name like ?   limit ?  , ?  ";

        int start = (pageInfo.getPageNo()-1) * pageInfo.getPageSize() ;
        List<Menu> list =  menuDao.selectList(sql,menuName, start,pageInfo.getPageSize());



        queryChild(list);

        String sql2 = "  select count(1) from menu    where parent_id is null and  active = 1 and menu_name like ?   ";
        long count =  menuDao.selectCount(sql2,menuName);

        pageInfo.setList(list);
        pageInfo.setTotalCount((int) count);

        return pageInfo;
    }


    /**
     * 查询子菜单
     * @param list
     */
    private void queryChild(List<Menu> list) {

        for (Menu menu : list) {

            String sql = " select * from menu where  active = 1 and parent_id = ?";
            List<Menu> children = menuDao.selectList(sql, menu.getMenuId());
            queryChild(children);
            menu.setChildren(children);
        }
    }

    @Override
    public Menu queryById(Integer id) {
        return menuDao.selectById(id);
    }

    @Override
    public boolean edit(Menu menu) {
        return false;
    }


    /**
     * 逻辑删除，没有真正删除数据
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        return  menuDao.deleteById(id);
    }

    @Override
    public boolean save(Menu menu) {
        return menuDao.insert(menu);
    }

    @Override
    public boolean batchdel(String[] ids) throws Exception {
        return false;
    }

    public boolean update(Menu menu) {
        return menuDao.update(menu);
    }

    public List<Menu> getUserMenus(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
//
        String sql = "SELECT distinct  \n" +
                "  m.* \n" +
                "FROM\n" +
                "  menu m \n" +
                "  INNER JOIN role_menu rm \n" +
                "    ON m.menu_id = rm.menu_id \n" +
                "  INNER JOIN user_role ur \n" +
                "    ON rm.role_id = ur.role_id \n" +
                "WHERE m.menu_type= 1 and m.parent_id is null AND ur.user_id =  ?   ";

        List<Menu> list = menuDao.selectList(sql, user.getUserId());
        queryChild(list);//查询子菜单


        return list ;
    }




    //查询用户有权限的 菜单 集合
    public List<Menu> getUserAllMenus(Integer userId) {

        String sql = "SELECT distinct  \n" +
                "  m.* \n" +
                "FROM\n" +
                "  menu m \n" +
                "  INNER JOIN role_menu rm \n" +
                "    ON m.menu_id = rm.menu_id \n" +
                "  INNER JOIN user_role ur \n" +
                "    ON rm.role_id = ur.role_id \n" +
                "WHERE m.menu_type= 2  AND ur.user_id =  ?   ";

        List<Menu> list = menuDao.selectList(sql, userId);

        return list ;
    }
}
